package com.peek.controller;

import com.peek.bean.Tree;
import com.peek.util.FileSizeUtil;
import com.peek.util.HelpMe;
import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.system.OsInfo;
import com.xiaoleilu.hutool.system.SystemUtil;
import com.xiaoleilu.hutool.util.NetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mengdexuan on 2017/6/22 11:13.
 */
@Controller
@RequestMapping("/tree")
public class FileController {

	private static Logger log = LoggerFactory.getLogger(FileController.class);
	private static OsInfo osInfo = SystemUtil.getOsInfo();


	@RequestMapping("/")
	public String index(HttpServletRequest request,Model model){

		model.addAttribute("serverTime",System.currentTimeMillis());
		model.addAttribute("serverIp", NetUtil.getLocalhostStr());

		return "file";
	}


	@RequestMapping("/async")
	@ResponseBody
	public List<Tree> async(HttpServletRequest request, Tree tree){

		long time1 = System.currentTimeMillis();

		String id = tree.getId();
		Integer level = tree.getLevel();
		log.info("节点 id : {}，节点层级：{}。",new Object[]{id,level});
		List<Tree> result = new ArrayList<>();

		if (HelpMe.isNull(id)){
			File[] files = File.listRoots();//系统根目录
			for(int i = 0; i < files.length; i++) {
				Tree head = new Tree();
				head.setName(files[i].toString());
				head.setId(files[i].toString());
				head.setIsParent(true);
				result.add(head);
			}
		}else {
			File[] fileArr = FileUtil.ls(id);

			if (HelpMe.isNotNull(fileArr)){
				for (File file:fileArr){
					Tree tmp = new Tree();
					if (tree.getIsParent()!=null&&tree.getIsParent()){
						try{
							long size = FileUtil.size(file);
							tmp.setLastModifiedTime(HelpMe.formatDate2StrTime(FileUtil.lastModifiedTime(file),HelpMe.yyyy_MM_dd_HH_mm_ss));
							String sizeStr = FileSizeUtil.FormetFileSize(size);
							tmp.setSize(size);
							tmp.setSizeStr(sizeStr);
						}catch (Exception e){}
					}
					tmp.setName(file.getName());
					tmp.setId(file.getPath());
					tmp.setIsParent(file.isDirectory());
					result.add(tmp);
				}
			}
		}
		long time2 = System.currentTimeMillis();

		log.info("查询耗时 --> "+(time2-time1)+" (ms)");
		return result;
	}




	@RequestMapping("/createDir")
	@ResponseBody
	public Boolean createDir(String currDir,String fileName,String dirId){

		if (HelpMe.isNull(dirId)){//添加
			String path = currDir+osInfo.getFileSeparator()+fileName;
			log.info("创建目录 --> "+path);
			FileUtil.mkdir(path);
		}else {//修改
			String extName = FileUtil.extName(dirId);
			String newPath = "";
			if (HelpMe.isNull(extName)){
				newPath = currDir + osInfo.getFileSeparator() +fileName;
			}else {
				newPath = currDir + osInfo.getFileSeparator() +fileName + "." + extName;
			}
			File file = new File(dirId);
			file.renameTo(new File(newPath));
			log.info("修改文件(夹)名称 {} --> {}",new Object[]{dirId,newPath});
		}


		return true;
	}



	@RequestMapping("/delDir")
	@ResponseBody
	public Boolean delDir(String dirIdList){

		List<String> idList = HelpMe.easySplit(dirIdList);

		if (HelpMe.isNotNull(idList)){
			for (String path:idList){
				FileUtil.del(path);
				log.info("删除文件(夹) --> "+path);
			}
		}

		return true;
	}







}






































